package com.cloud.system.controller;

import com.github.pagehelper.PageHelper;
import com.cloud.common.constants.ExcelPoiConstants;
import com.cloud.common.domain.Result;
import com.cloud.common.enums.CheckType;
import com.cloud.controller.BaseController;
import com.cloud.entity.SysRole;
import com.cloud.entity.SysUser;
import com.cloud.enums.BusinessType;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.user_info.entity.vo.ProjectRoleDao;
import com.cloud.user_info.service.ISysRoleService;
import com.cloud.user_info.service.ISysUserService;
import com.cloud.utils.ExcelPoiUtils;
import com.cloud.utils.SecurityUtils;
import com.cloud.utils.ServletUtils;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理
 *
 * @author yzj
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserService userService;

    @Operation(summary = "查询角色列表(分页)")
    @RequestPermissions("system:role:list")
    @GetMapping("/list")
    public Result list(SysRole role) {
        startPage();
        role.setUserId(SecurityUtils.getUserId());
        role.setSysCode(SecurityUtils.getSysCode());
        List<SysRole> list = roleService.selectRoleList(role);
        return getDataTable(list);
    }

    @Operation(summary = "查询角色列表")
    @GetMapping("/allList")
    public Result allList(SysRole role) {
        role.setSysCode(SecurityUtils.getSysCode());
        return Result.success("查询成功", roleService.selectRoleList(role));
    }

    @Operation(summary = "查询已分配用户角色列表(分页)")
    @RequestPermissions("system:role:list")
    @GetMapping("/allRoleAuthList")
    public Result allRoleAuthList(ProjectRoleDao projectRole) {
        startPage();
        List<SysUser> list = userService.selectRoleAuthList(projectRole);
        return getDataTable(list);
    }

    @Operation(summary = "导出角色列表")
    @Logs(title = "导出角色信息", businessType = BusinessType.EXPORT)
    @RequestPermissions("system:role:export")
    @GetMapping("/export")
    public void export(SysRole role) {
        role.setSysCode(SecurityUtils.getSysCode());
        PageHelper.startPage(ExcelPoiConstants.startNum, ExcelPoiConstants.maxSize);
        List<SysRole> list = roleService.selectRoleList(role);
        String titleName = "系统角色列表";
        ExcelPoiUtils.exportExcel(list, titleName, "sys_role",
                SysRole.class, titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "查询角色详细")
    @RequestPermissions("system:role:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long roleId) {
        return Result.success("查询成功", roleService.selectRoleById(roleId));
    }

    @Operation(summary = "新增角色信息")
    @Logs(title = "新增角色信息", businessType = BusinessType.INSERT)
    @RequestPermissions("system:role:add")
    @PostMapping("/add")
    public Result insert(@RequestBody SysRole role) {
        if (CheckType.NOT_UNIQUE.getCode().equals(roleService.checkRoleNameUnique(role))) {
            return Result.error("新增角色[" + role.getRoleName() + "]失败,角色名称已存在");
        } else if (CheckType.NOT_UNIQUE.getCode().equals(roleService.checkRoleKeyUnique(role))) {
            return Result.error("新增角色[" + role.getRoleName() + "]失败,角色权限已存在");
        }
        role.setUserId(SecurityUtils.getUserId());
        role.setSysCode(SecurityUtils.getSysCode());
        return roleService.insertRoleInfo(role);
    }

    @Operation(summary = "修改角色信息")
    @Logs(title = "修改角色信息", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:role:edit")
    @PostMapping("/edit")
    public Result update(@RequestBody SysRole role) {
        if (CheckType.NOT_UNIQUE.getCode().equals(roleService.checkRoleNameUnique(role))) {
            return Result.error("修改角色[" + role.getRoleName() + "']失败,角色名称已存在");
        } else if (CheckType.NOT_UNIQUE.getCode().equals(roleService.checkRoleKeyUnique(role))) {
            return Result.error("修改角色[" + role.getRoleName() + "]失败,角色权限已存在");
        }
        role.setUserId(SecurityUtils.getUserId());
//        role.setSysCode(SecurityUtils.getSysCode());
        return roleService.updateRoleInfo(role);
    }

    @Operation(summary = "修改角色数据权限")
    @Logs(title = "修改角色数据权限", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:role:edit")
    @PostMapping("/dataScope")
    public Result dataScope(@RequestBody SysRole role) {
        return toResult(roleService.authDataScope(role));
    }

    @Operation(summary = "角色状态修改")
    @Logs(title = "修改角色状态", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:role:edit")
    @PostMapping("/changeStatus")
    public Result changeStatus(@RequestBody SysRole role) {
        return toResult(roleService.updateRoleStatus(role));
    }

    @Operation(summary = "批量删除角色信息")
    @Parameter(name = "roleIds", description = "角色ID[多个用,隔开]")
    @Logs(title = "删除角色信息", businessType = BusinessType.DELETE)
    @RequestPermissions("system:role:remove")
    @PostMapping("/remove")
    public Result remove(String roleIds) {
        List<Long> list = ToolUtils.setIdsToList(roleIds);
        return toResult(roleService.deleteRoleByIds(list));
    }

    @Operation(summary = "Feign内部调用(查询角色详细)")
    @RequestMapping(value = "/inAuth/getInRoleInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public Result getInRoleInfo(Long roleId) {
        return Result.success("查询成功", roleService.selectRoleById(roleId));
    }

    @Operation(summary = "Feign内部调用(根据ID查询关联用户ID数组)")
    @RequestMapping(value = "/inAuth/getRoleUserIdsById", method = {RequestMethod.POST, RequestMethod.GET})
    public Result getRoleUserIdsById(String roleIds) {
        String sysCode = SecurityUtils.getSysCode();
        return Result.success("查询成功", roleService.getRoleUserIdsById(sysCode, roleIds));
    }

}

